package cn.com.dashihui.web.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.web.dao.ContentsCategory;

public class ContentsCategoryService {

	public Page<Record> findByPage(int pageNum, int pageSize) {
		StringBuffer sb=new StringBuffer("FROM t_contents_category");
		return Db.paginate(pageNum, pageSize, "SELECT * ", sb.toString());
	}

	

	public boolean AddCategory(ContentsCategory cc) {
		
		return cc.save();
	}

	public ContentsCategory findById(int id) {
		
		return ContentsCategory.me().findFirst("SELECT * FROM t_contents_category WHERE id="+id);
	}

	public boolean editCategory(ContentsCategory cc) {
		
		return cc.update();
	}
	public boolean findExistSubject(int id){
		boolean flag=true;
		List<Record> w=Db.find("SELECT * FROM t_contents_subject WHERE category_id="+id+" limit 1");
		if(w.size()==0){
			flag=false;
		}
		return flag;
		
	}
	
	public boolean deleteCategory(int id) {
		List<String> l=new ArrayList<String>();
		l.add("DELETE FROM t_contents_category WHERE id="+id);
		Db.batch(l, l.size());
		
		return true;
	}

	public boolean sortCategory(Map<String, String> sortMap) {
		int batchSize = sortMap.size();
		List<String> sqlList = new ArrayList<String>();
		for (String id : sortMap.keySet()) {
			sqlList.add("UPDATE t_contents_category SET orderNo="+sortMap.get(id)+" WHERE id="+id);
		}
		int[] result = Db.batch(sqlList, batchSize);
		return result.length > 0;
		
	}

}
